package com.yuke.cloud.common.util;


import cn.hutool.core.date.DateField;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * The class Date util.
 *
 * @author
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateUtil {

	/**
	 * 获取系统前时间.
	 *
	 * @param minute the minute
	 *
	 * @return the before time[yyyy-MM-dd HH:mm:ss]
	 */
	public static String getBeforeTime(int minute) {
		Date newDate = cn.hutool.core.date.DateUtil.offset(new Date(), DateField.MINUTE, -minute);
		return cn.hutool.core.date.DateUtil.formatDateTime(newDate);
	}

	// 获取当天0点时间戳 Created by Wenhui Huang 2019/02/28
	public static Long dayTimeInMillis() {
		Calendar calendar = Calendar.getInstance();// 获取当前日期
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Long time = calendar.getTimeInMillis();
		return time;
	}

	// 获取当月0点时间戳 Created by Wenhui Huang 2019/02/28
	public static Long monthTimeBeginInMillis() {
		Calendar calendar = Calendar.getInstance();// 获取当前日期
		calendar.add(Calendar.YEAR, 0);
		calendar.add(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Long time = calendar.getTimeInMillis();
		return time;
	}
	public static Date monthTimeBegin(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	// 获取当月23.59.59时间戳
	public static Long monthTimeEndInMillis() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		long tt = calendar.getTime().getTime() / 1000;
		return tt;
	}
	public static Date monthTimeEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}


	// 获取当年0点时间戳 Created by Wenhui Huang 2019/02/28
	public static Long yearTimeInMillis() {
		Calendar calendar = Calendar.getInstance();// 获取当前日期
		calendar.add(Calendar.YEAR, 0);
		calendar.add(Calendar.DATE, 0);
		calendar.add(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Long time = calendar.getTimeInMillis();
		return time;
	}

	public static String getStringDate(Date currentTime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	//获取到上个月第一天
	public static Date lastMonthTimeBegin(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH,-1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	//获取到上个月最后一天
	public static Date lastMonthTimeEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH,0);
		calendar.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
		calendar.set(Calendar.MINUTE, 59); //将分钟至59
		calendar.set(Calendar.SECOND,59); //将秒至59
		calendar.set(Calendar.MILLISECOND, 59); //将毫秒至59
		return calendar.getTime();
	}

	//获取到下个月第一天
	public static Date nextMonthTimeBegin(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH,+1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	//获取到下个月最后一天
	public static Date nextMonthTimeEnd(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH,+1);
		calendar.set(Calendar.DAY_OF_MONTH,0);
		calendar.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
		calendar.set(Calendar.MINUTE, 59); //将分钟至59
		calendar.set(Calendar.SECOND,59); //将秒至59
		calendar.set(Calendar.MILLISECOND, 59); //将毫秒至59
		return calendar.getTime();
	}
	//获取上一天
	public static Date lastDayTimeBegin(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE,-1);
		calendar.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
		calendar.set(Calendar.MINUTE, 0); //将分钟至0
		calendar.set(Calendar.SECOND,0); //将秒至0
		calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0
		return calendar.getTime();
	}

	//获取今天开始时间
	public static Date todayTimeBegin(){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
		calendar.set(Calendar.MINUTE, 0); //将分钟至0
		calendar.set(Calendar.SECOND,0); //将秒至0
		calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0
		return calendar.getTime();
	}

	//获取明天开始时间
	public static Date nextDayTimeBegin(){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE,+1);
		calendar.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
		calendar.set(Calendar.MINUTE, 0); //将分钟至0
		calendar.set(Calendar.SECOND,0); //将秒至0
		calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0
		return calendar.getTime();
	}

	//获取num天前开始时间
	public static Date numDayTimeBegin(Integer num){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE,-num);
		calendar.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
		calendar.set(Calendar.MINUTE, 0); //将分钟至0
		calendar.set(Calendar.SECOND,0); //将秒至0
		calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0
		return calendar.getTime();
	}

	/**
	 * 日期比较date2比date1多的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date1,Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		// 不同年
		if(year1 != year2)
		{
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++)
			{	//闰年
				if(i%4==0 && i%100!=0 || i%400==0)
				{
					timeDistance += 366;
				}
				else //不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2-day1) ;
		}
		else // 同一年
		{
			return day2-day1;
		}
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDaysByMillisecond(Date date1,Date date2)
	{
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
		return days;
	}

	//获取指定日期的当天开始时间的时间戳
	public static Date getDateTimeBegin(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
		calendar.set(Calendar.MINUTE, 0); //将分钟至0
		calendar.set(Calendar.SECOND,0); //将秒至0
		calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0
		return calendar.getTime();
	}

	//获取指定日期的
	public static Date getDateTimeEnd(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,+1);
		calendar.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
		calendar.set(Calendar.MINUTE, 0); //将分钟至0
		calendar.set(Calendar.SECOND,0); //将秒至0
		calendar.set(Calendar.MILLISECOND, 0); //将毫秒至0
		return calendar.getTime();
	}
}
